package ThreadingSafety;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.PriorityBlockingQueue;

/**
 *  定时器 在中的扫描线程 使用线程池
 */

// 表示一个任务
class MyTask2 implements Comparable<MyTask2>{
    // 要实现一个比较器, 才可以进行比较.
    @Override
    public int compareTo(MyTask2 o) {
        return (int) (this.time - o.time);
    }

    // 要执行的具体任务
    private final Runnable runnable;
    // 什么时间执行任务(time是一个时间戳)
    private final long time;

    public MyTask2(Runnable runnable, long delay) {
        this.runnable = runnable;
        // 时间戳 毫秒级别.
        this.time = System.currentTimeMillis() + delay;
    }

    public Runnable getRunnable() {
        return runnable;
    }

    public long getTime() {
        return time;
    }

}

// 我的定时器
class MyTimer2 {
    // 使用 优先级阻塞队列 ~~
    private final BlockingQueue<MyTask2> queue = new PriorityBlockingQueue<>();

    private final Object locker = new Object();

    public MyTimer2() {
        // 运用线程池.
        ExecutorService pool = Executors.newCachedThreadPool();
        pool.submit(new Runnable() {
            @Override
            public void run() {
                while(true) {
                    try {
                        synchronized (locker) {
                            MyTask2 task = queue.take();
                            long curTime = System.currentTimeMillis();
                            if (curTime >= task.getTime()) {
                                // 到点了, 执行相关任务.
                                task.getRunnable().run();
                            } else {
                                queue.put(task);
                                locker.wait(task.getTime() - curTime);
                            }
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    public void schedule(Runnable runnable, long after) throws InterruptedException {
        // 将 定时器安排的任务放入 优先级阻塞队列中.
        MyTask2 task = new MyTask2(runnable,after);
        queue.put(task);
        synchronized (locker) {
            // 当插入新任务时, 立刻让唤醒wait,因为新任务的执行时间可能比正在wait的任务短.
            locker.notify();
            // 还需要注意一个问题, 万一执行wait之前调用notify就可能出问题!!!(9/18下午 01:33:00处)
        }
    }

}

public class TestDemo26 {
    public static void main(String[] args) throws InterruptedException {
        MyTimer2 timer = new MyTimer2();
        timer.schedule(new Runnable() {
            final long start = System.currentTimeMillis();
            @Override
            public void run() {
                System.out.println("时间到1!");
                long end = System.currentTimeMillis();
                System.out.println((end - start) + " ms");
            }
        },3000);
        System.out.println("开始计时!");

        timer.schedule(new Runnable() {
            final long start = System.currentTimeMillis();
            @Override
            public void run() {
                System.out.println("时间到2!");
                long end = System.currentTimeMillis();
                System.out.println((end - start) + " ms");
            }
        },3000);

        timer.schedule(new Runnable() {
            final long start = System.currentTimeMillis();
            @Override
            public void run() {
                System.out.println("时间到3!");
                long end = System.currentTimeMillis();
                System.out.println((end - start) + " ms");
            }
        },3000);

    }
}

// TestDemo24,TestDemo25 中将介绍 线程池